<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img class="logo" src="../assets/avatar.png" alt="logo" />
        <span>VUE 后台管理系统 (学习版)</span>
        <span class="sub-title">2020年10月国庆期间</span>
      </div>
      <el-button type="primary" @click="logout"> 退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 侧边栏菜单区域 -->
        <div class="toggle-button" @click="toggleCollapse" 
          :class="toggleButton">
          <i class="fa fa-bars" ></i>
        </div>
        <el-menu
          background-color="#1b3c5e"
          text-color="#fff"
          active-text-color="#409eff"
          class="el-menu-vertical-demo"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu
            :index="'/' + item.path"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="saveNavState('/' + subitem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>
                  {{ subitem.authName }}
                </span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconsObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao",
      },
      isCollapse: false,
      activePath: ''
    };
  },
  computed:{
    toggleButton(){
      // 调整按钮使得收缩时按钮与图标对齐，展开时与下方箭头对齐
      return this.isCollapse? 'buttonCollapse':'';
    }
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },
    // 切换菜单折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存活动菜单
    saveNavState(activepath){
      window.sessionStorage.setItem('activePath', activepath);
      this.activePath = activepath;
    }
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: #2b73d1;
  color: rgb(241, 238, 238);
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  /* padding-left: 0; */
  align-items: center;
  font-size: 25px;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 25px;
      font-size: 25px;
      // letter-spacing: 5px;
      
    }
  }
}

.sub-title{
  font-size: 16px!important;
  margin-top: 12px;
}

.logo {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  padding: 4px;
  box-shadow: 0 0 2px #1e559d;
  background-color: #4d9aeade;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}

.home-container {
  height: 100%;
}

.el-menu {
  height: calc(100% - 30px);
  border-right: none;
}

.is-active {
  background-color: #18b625;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #163654;
  height: 30px;
  line-height: 30px;
  color: rgb(196, 199, 202);
  text-align: right;
  padding-right: 18px;
  cursor: pointer;
}

.buttonCollapse {
  padding-right: 28px;
}
</style>
